package com.sskj.base.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.base.R;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.entry.PankouData;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.NumberUtil;

import java.util.ArrayList;
import java.util.List;


public class PankouView extends LinearLayout {

    private View contentView;

    private RecyclerView buyList;
    private RecyclerView sellList;

    private TextView priceTextCN;
    private TextView priceText;

    private List<PankouData.Data> sellData;
    private List<PankouData.Data> buyData;

    private BaseAdapter<PankouData.Data> buyAdapter;
    private BaseAdapter<PankouData.Data> sellAdapter;

    private int buyColor = R.color.green;
    private int sellColor = R.color.red;
    private OnSelectListener listener;

    public PankouView(Context context) {
        super(context);
        init();
    }

    public PankouView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PankouView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (contentView == null) {
            contentView = LayoutInflater.from(getContext()).inflate(R.layout.pankou_view, null);
            buyList = contentView.findViewById(R.id.buy_list);
            sellList = contentView.findViewById(R.id.sell_list);
            priceText = contentView.findViewById(R.id.price);
            priceTextCN = contentView.findViewById(R.id.price_cn);
            buyList.setLayoutManager(new LinearLayoutManager(getContext()));
            sellList.setLayoutManager(new LinearLayoutManager(getContext()));
            addView(contentView);

        }
        buyAdapter = new BaseAdapter<PankouData.Data>(R.layout.item_pankou_view, buyData, buyList) {
            @Override
            public void bind(ViewHolder holder, PankouData.Data item) {
                holder.setText(R.id.item_pankou_id, holder.getAdapterPosition() + 1 + "");
                holder.setText(R.id.item_pankou_price, NumberUtil.keep4(item.getPrice()));
                holder.setText(R.id.item_pankou_num, NumberUtil.keep4(item.getTotalSize()));
                holder.setTextColor(R.id.item_pankou_price, ContextCompat.getColor(getContext(), buyColor));
                holder.setTextColor(R.id.item_pankou_num, ContextCompat.getColor(getContext(), buyColor));
                if (item.isShow()) {
                    colorAnim(holder.getView(R.id.item_pankou_price));
                    colorAnim(holder.getView(R.id.item_pankou_num));
                }
                ClickUtil.click(holder.getView(R.id.item_pankou_price), v -> {
                    if (listener != null) {
                        listener.onSelect(item.getPrice());
                    }
                });
            }
        };

        sellAdapter = new BaseAdapter<PankouData.Data>(R.layout.item_pankou_view, sellData, sellList) {
            @Override
            public void bind(ViewHolder holder, PankouData.Data item) {
                holder.setText(R.id.item_pankou_id, holder.getAdapterPosition() + 1 + "");
                holder.setText(R.id.item_pankou_price, NumberUtil.keep4(item.getPrice()));
                holder.setText(R.id.item_pankou_num, NumberUtil.keep4(item.getTotalSize()));
                holder.setTextColor(R.id.item_pankou_price, ContextCompat.getColor(getContext(), sellColor));
                holder.setTextColor(R.id.item_pankou_num, ContextCompat.getColor(getContext(), sellColor));
                if (item.isShow()) {
                    colorAnim(holder.getView(R.id.item_pankou_price));
                    colorAnim(holder.getView(R.id.item_pankou_num));
                }
                ClickUtil.click(holder.getView(R.id.item_pankou_price), v -> {
                    if (listener != null) {
                        listener.onSelect(item.getPrice());
                    }
                });
            }
        };

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        contentView.setLayoutParams(new LayoutParams(getMeasuredWidth(), getMeasuredHeight()));
    }

    public void setData(PankouData data) {
        if (data != null) {
            buyData = data.getAsks();
            sellData = data.getBids();
        }
        if (buyData == null) {
            buyData = new ArrayList<>();
        }
        if (sellData == null) {
            sellData = new ArrayList<>();
        }
        while (buyData.size() < 5) {
            PankouData.Data item = new PankouData.Data();
            item.setPrice("---");
            item.setTotalSize("---");
            buyData.add(item);
        }
        while (sellData.size() < 5) {
            PankouData.Data item = new PankouData.Data();
            item.setPrice("---");
            item.setTotalSize("---");
            sellData.add(item);
        }

        if (sellAdapter != null) {
            sellAdapter.setNewData(sellData);
        }

        if (buyAdapter != null) {
            buyAdapter.setNewData(buyData);
        }

    }


    public void update(PankouData data) {
        if (data != null) {
            if (buyAdapter != null && data.getAsks() != null) {
                while (sellData.size() < 5) {
                    PankouData.Data item = new PankouData.Data();
                    item.setPrice("---");
                    item.setTotalSize("---");
                    data.getAsks().add(item);
                }
                buyAdapter.setNewData(data.getAsks());
            }

            if (sellAdapter != null && data.getBids() != null) {
                while (data.getAsks().size() < 5) {
                    PankouData.Data item = new PankouData.Data();
                    item.setPrice("---");
                    item.setTotalSize("---");
                    data.getBids().add(item);
                }
                sellAdapter.setNewData(data.getBids());
            }

        }

    }


    public void colorAnim(TextView textView) {
        ValueAnimator animator = ValueAnimator.ofFloat(0, 3);
        animator.setDuration(300);
        animator.setRepeatCount(0);
        animator.addUpdateListener(animation -> {
            float x = (float) animation.getAnimatedValue();
            if (x == 0 && getContext() != null) {
                textView.setBackgroundColor(getResources().getColor(R.color.tip));
            } else if (x == 3 && getContext() != null) {
                textView.setBackgroundColor(getResources().getColor(R.color.background));
            }
        });
        animator.start();
    }

    public interface OnSelectListener {
        void onSelect(String price);
    }

    public void setOnSelectListner(OnSelectListener listener) {
        this.listener = listener;
    }
}
